/**
 * @desc tools
 * @author MiyueFE <https://github.com/miyuesc>
 * @since 2024/8/1 下午4:32
 */
import { Modal } from '@arco-design/web-vue'
import type { ModalConfig } from '@arco-design/web-vue/es/modal/interface'

export function capitalize(str: string): string {
  if (!str || str.length === 0)
    return ''
  const lower = str
  return lower.substring(0, 1).toUpperCase() + lower.substring(1, lower.length)
}

export function modalValidator(title: string, message: string, config: Partial<ModalConfig> = {}) {
  return new Promise<boolean>((resolve) => {
    Modal.confirm({
      title,
      content: message,
      okText: '确认',
      cancelText: '取消',
      ...config,
      onOk: () => {
        resolve(true)
      },
      onCancel: () => {
        resolve(false)
      },
    })
  })
}

/**
 * 树形数据转换
 */
export function treeDataTranslate(data: any, id: string = 'id', pid: string = 'parentId'): any[] {
  const res: any[] = []
  const temp = {}
  for (let i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (let k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]].children) {
        temp[data[k][pid]].children = []
      }
      if (!temp[data[k][pid]]._level) {
        temp[data[k][pid]]._level = 1
      }
      data[k]._level = temp[data[k][pid]]._level + 1
      temp[data[k][pid]].children.push(data[k])
    }
    else {
      res.push(data[k])
    }
  }
  return res
}

export function filterTreeData(treeData: any[], filterKey: string, keyword?: string): any[] {
  if (!keyword)
    return treeData

  const loop = (data: any[]) => {
    const result: any[] = []
    data.forEach((item) => {
      if (item[filterKey]?.toLowerCase().indexOf(keyword.toLowerCase()) > -1) {
        result.push({ ...item })
      }
      else if (item.children) {
        const filterData = loop(item.children)
        if (filterData.length) {
          result.push({
            ...item,
            children: filterData,
          })
        }
      }
    })
    return result
  }
  return loop(treeData)
}
